//index.js
const { getItem, setItem } = require('../../utils/store')
//获取应用实例
const app = getApp()

Page({
  data: {
    userId: getItem('userid'),
    userInfo: getItem('user'),
  },
  getUserInfo(e) {
    const userInfo = e.detail.userInfo
    userInfo.openid = getItem('openid')
    console.log('通过整合后 userInfo 去后端请求 userId', userInfo)
    console.log('模拟请求到 userid ', 1001)
    setItem('userid', 1001)
    setItem('user', userInfo)
    this.setData({
      userId: 1001,
      userInfo: userInfo,
    })
  },
  onLoad: function () {
    if (!this.data.userId) {
      wx.login({
        success: (result) => {
          if (result.code) {
            // 通过 code 去服务端 获取到 openid
            console.log('通过 code 去后端请求 openid ', result.code)
            console.log('返回的 openid ', 'abcdefg')
            setItem('openid', 'abcdefg')
          }
        },
        fail: () => {},
        complete: () => {},
      })
    }
  },
})
